export default {
  data() {
    return {
      confirm1: false, //是否确认交易用户
      confirm2: false, //是否确认交易车辆
      maiState: '', //1 我是买家  2 我是卖家
      carConfig: {}, //车辆信息
      activeConfig: {}, //选择了配置后的外观内饰列表
      exteriorColor: {}, //外观颜色信息
      dealUserInfo: {}, //交易用户信息
      userInfo: {}, //当前用户信息
      goInPage: '', //上级进入页面//buycar index
      identity: '', //用户身份 权限 CarStore 第一大类（4s店、汽贸店、自营卖车）：4s店、汽贸店、7迈直营车
      //Factory 第二大类（主机+车源商）：车源商、主机厂
      // BeautyStore 第三大类（养护自营+加盟门店）：美保护精品供应商、7迈直营美护保
      // RepairStore 第四大类（维保货源品牌+经销商）：养护门店、7迈直营养护
      dealcarMsg: {},
      goodsid: '', //商品id
      colorstatus: false, //buycar 入口
      colorShow: false, //颜色弹出框
    }
  },
  filters: {
    cutString(str, len) {
      if (str.length * 2 <= len) {
        return str
      }
      let strlen = 0
      let s = ''
      for (let i = 0; i < str.length; i++) {
        s = s + str.charAt(i)
        if (str.charCodeAt(i) > 128) {
          strlen = strlen + 2
          if (strlen >= len) {
            return s.substring(0, s.length - 1) + '...'
          }
        } else {
          strlen = strlen + 1
          if (strlen >= len) {
            return s.substring(0, s.length - 2) + '...'
          }
        }
      }
      return s
    },
  },
  created() {
    this.identity = this.$store.state.user.userInfo.permious
    // 当前用户信息
    this.userInfo = this.$store.state.user.userInfo
    // console.log( '当前用户信息',this.userInfo)
    this.getBrandDataGroup()

  },
  onLoad(options) {

    this.goInPage = options.page

    if (this.goInPage === 'buycar') {

      this.maiState = 1
      this.confirm1 = true
      this.dealcarMsg = JSON.parse(decodeURIComponent(options.datalist))
      console.log(21, this.dealcarMsg)
      this.getDealUserMsg(this.dealcarMsg.storeId)
      this.activeConfig = this.dealcarMsg.colorlist
      this.goodsid = this.dealcarMsg.id
      this.carConfig = this.dealcarMsg
      delete this.carConfig.colorlist
    }
  },
  onShow() {
    // 获取交易用户
    uni.$on('selectUser', data => {
      this.dealUserInfo = {}
      let userObj = Object.assign(this.dealUserInfo, data)
      this.dealUserInfo = userObj
      console.log('选择用户信息', this.dealUserInfo.id)
    })
    // 获取外观颜色
    uni.$on('exteriorColor', data => {
      this.exteriorColor = {}
      Object.assign(this.exteriorColor, data)
      this.exteriorColor = this.exteriorColor
      // console.log(' 获取外观颜色',this.exteriorColor )
    })
    // 获取车型
    uni.$on('specifAll', data => {
      let newlist = Object.assign(this.carConfig, data)
      // console.log('获取车型',newlist)
      this.$nextTick(() => {
        this.carConfig = newlist
        this.exteriorColor = {}
      })
      this.carConfig = this.$store.state.carConfig
      this.activeConfig = this.$store.state.activeConfig
      // console.log(this.carConfig,this.activeConfig  )
    })

  },
  onHide(options) {
    // console.log('页面离开')
  },
  computed: {
    // 判断空值
    dealUserInfostatus() {
      if (JSON.stringify(this.dealUserInfo) === '{}') {
        return false
      } else {
        return true
      }
    },
    // 判断空值
    exteriorColorStutus() {
      console.log(JSON.stringify(this.exteriorColor) === '{}')
      if (JSON.stringify(this.exteriorColor) === '{}') {
        return false
      } else {
        return true
      }
    },
    // 判断是否完善交易信息
    canGoPay() {
      // console.log(this.confirm1 , this.confirm2 , Object.keys(this.carConfig).length > 0 ,
      //   Object.keys(this.exteriorColor).length > 0 ,
      //   Object.keys(this.dealUserInfo).length > 0)
      if (this.maiState !== '' && this.confirm1 && this.confirm2 && Object.keys(this.carConfig).length > 0 &&
        Object.keys(this.exteriorColor).length > 0 &&
        Object.keys(this.dealUserInfo).length > 0) {
        return true
      } else {
        return false
      }
    }
  },
  watch: {
    // dealUserInfo(val) {
    //   this.dealUserInfo = val
    //   console.log(typeof val,val)
    // }
  },
  methods: {
    // 车源详情入口  获取交易用户信息 //卖家
    async getDealUserMsg(stotrId) {

      const res = await this.$api.carOrder.StartContractDataListAsync({
        storeid: stotrId
      })
      try {
        console.log(res)
        if (res.code === 200) {
          this.dealUserInfo = res.data.list[0]
        }
      } catch (e) {}
    },

    gos(item) {
      uni.navigateTo({
        url: '/pages_order/' + item + '/' + item
      })
    },
    //品牌
    async getBrandDataGroup() {
      try {
        const res = await this.$api.jsonData.brandDataGroup()
      } catch (e) {}
    },
    // 判断能否选外观内饰
    selectExteriorInterior() {
      console.log(11111, this.activeConfig)
      if (JSON.stringify(this.activeConfig) !== '{}') {
        uni.navigateTo({
          url: '/pages_common/exteriorInterior/exteriorInterior?colorList=' + encodeURIComponent(JSON.stringify(
            this.activeConfig))
        })
      } else {
        console.log(3333)
        uni.showToast({
          title: '请先选择车型/款式',
          duration: 2000,
          icon: 'none'
        })
      }
    },
    // 车源详情入口选择颜色
    selectColor(it) {
      let a = {}
      a.colorList = it
      this.exteriorColor = a
      this.colorShow = false
      console.log(this.exteriorColor)
    },
    setToast() {
      uni.showToast({
        title: '请先完善交易信息并确认',
        duration: 2000,
        icon: 'none'
      })
    },
    goNext(url) {
      uni.navigateTo({
        url: url
      })
    },
    nogo() {},
    // 发起合同
    goInContract() {

      let carMsg = {
        brandlist: this.carConfig.brand,
        series: this.carConfig.Serieslist,
        configulist: this.carConfig.Configulist
      }

      if (this.goInPage === 'buycar') {
        uni.showLoading({
          title: '加载中'
        })
        let data = {
          buyeruserid: this.userInfo.tid, //买方用户id
          buyerstoreid: this.$store.state.userInfoDetail.store.storeId, //买方店铺id
          sellerstoreid: this.dealcarMsg.storeId, //卖方店铺id
          goodsid: this.dealcarMsg.id, //商品id
          color: JSON.stringify(this.exteriorColor.colorList), //车辆颜色
          // remarks: this.dealcarMsg.remarks
        }
        this.$api.carOrder.AffirmContractDataListTask(data).then(res => {
          console.log(res)
          if (res.code === 200) {
            res.data.list[0].maiState = this.maiState
            res.data.list[0].goInPage = this.goInPage
            res.data.list[0].remarks = this.dealcarMsg.remarks
            uni.navigateTo({
              url: '/pages_order/initiatingContract/initiatingContract?data=' + encodeURIComponent(JSON.stringify(
                res.data.list[0]))
            })
          }
          uni.hideLoading()
        }).catch(err => {
          uni.hideLoading()
        })

      } else {
        uni.showLoading({
          title: '加载中'
        })
        let data = {
          'BuyersStoreId': this.maiState === 1 ? this.userInfo.tid : this.dealUserInfo.id,
          'SellerStoreId': this.maiState === 2 ? this.userInfo.tid : this.dealUserInfo.id
        }
        this.$api.carOrder.GetAccountDataDataListTask(data).then(res => {

          if (res.code === 200) {
            res.data.list[0].maiState = this.maiState
            res.data.list[0].color = JSON.stringify(this.exteriorColor.colorList), //车辆颜色
            res.data.list[0].carMsg = JSON.stringify(carMsg)
            res.data.list[0].goInPage = this.goInPage,
            res.data.list[0].buystoreid = this.maiState === 1 ? this.$store.state.userInfoDetail.store.storeId
              : this.dealUserInfo.store_Id,
            res.data.list[0].sellstoreid = this.maiState === 2 ? this.$store.state.userInfoDetail.store.storeId
              : this.dealUserInfo.store_Id
            res.data.list[0].buyeruserid = this.maiState === 2 ? this.dealUserInfo.id : this.userInfo.tid,
            res.data.list[0].remarks = this.dealcarMsg.remarks
            // console.log(res.data.list[0])
            uni.hideLoading()
            uni.navigateTo({
              url: '/pages_order/initiatingContract/initiatingContract?data=' + encodeURIComponent(JSON.stringify(
                res.data.list[0]))
            })
          }
        }).catch(err => {
          uni.hideLoading()
        })
      }

      uni.$off('selectUser')
      uni.$off('exteriorColor')
      uni.$off('specifAll')
    }

  }
}
